package com.itheima.core.web.controller;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.common.utils.Page;
import com.itheima.core.po.BaseDict;
import com.itheima.core.po.Customer;
import com.itheima.core.po.User;
import com.itheima.core.service.BaseDictService;
import com.itheima.core.service.CustomerService;
/**
 * 客户管理控制器类
 */
@Controller
public class CustomerController {
	// 依赖注入
	@Autowired
	private CustomerService customerService;
	@Autowired
	private BaseDictService baseDictService;
	// 客户来源，@Value表示用resource.properties里的customer.from.type（002）表示新创建的变量FROM_TYPE
	@Value("${customer.from.type}")
	private String FROM_TYPE;
	// 客户所属行业,001
	@Value("${customer.industry.type}")
	private String INDUSTRY_TYPE;

	/**
	 *  客户列表
	 */
	@RequestMapping(value = "/customer/list.action")
	//默认页数为page第1页，一页显示10行数rows为10行，用户登入，显示所有的信息
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, 
			String stu_name, String stu_sex, String stu_cla,
			 Model model) {
		// 条件查询所有客户
		Page<Customer> customers = customerService
				.findCustomerList(page, rows, stu_name, 
						stu_sex, stu_cla);
		model.addAttribute("page", customers);
		// 动态查询客户来源(性别002)，到数据库basedict表中查询代表客户来源的列dict_type_code，并把其值添加到下拉列表，所以可以动态地通过修改basedict表自定义客户来源
		List<BaseDict> fromType = baseDictService
				.findBaseDictByTypeCode(FROM_TYPE);
		// 动态查询客户所属行业(班级001)
		List<BaseDict> industryType = baseDictService
				.findBaseDictByTypeCode(INDUSTRY_TYPE);
		// 添加参数
		//002
		model.addAttribute("fromType", fromType);
		//001
		model.addAttribute("industryType", industryType);
		//stu_name
		model.addAttribute("stuName", stu_name);
		//sex
		model.addAttribute("stuSex", stu_sex);
		//cla
		model.addAttribute("stuCla", stu_cla);
		
		return "customer";
	}
	
	/**
	 * 创建客户，并加上创建者id和创建时间项
	 */
	@RequestMapping("/customer/create.action")
	@ResponseBody
	public String customerCreate(Customer customer,HttpSession session) {
	    // 获取Session中的当前用户信息，指登录的用户信息
	    //User user = (User) session.getAttribute("USER_SESSION");
	    // 将当前用户id存储在客户对象中
	    //customer.setCust_create_id(user.getUser_id());
	    // 创建Date对象
	    Date date = new Date();
	    // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
	    Timestamp timeStamp = new Timestamp(date.getTime());
	    customer.setStu_createtime(timeStamp);
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = customerService.createCustomer(customer);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

	@RequestMapping("/customer/select.action")

	public String findstubyid(Customer customer,Model model) {
		Customer resultUser=customerService.findstubyid(customer);
		if(resultUser==null) {
			model.addAttribute("errorMsg", "此用户不存在！");
			return "StudentFind";
		}else {
			model.addAttribute("resultUser",resultUser);
			return "StudentFind";
		}
	}
	/**
	 * 通过id获取客户信息
	 */
	@RequestMapping("/customer/getCustomerById.action")
	@ResponseBody
	public Customer getCustomerById(Integer id) {
	    Customer customer = customerService.getCustomerById(id);
	    return customer;
	}
	/**
	 * 更新客户
	 */
	@RequestMapping("/customer/update.action")
	@ResponseBody
	public String customerUpdate(Customer customer) {
	    int rows = customerService.updateCustomer(customer);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

	/**
	 * 删除客户
	 */
	@RequestMapping("/customer/delete.action")
	@ResponseBody
	public String customerDelete(Integer id) {
	    int rows = customerService.deleteCustomer(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
	@RequestMapping("/customer/tolyx.action")

	public String tolyx(HttpServletRequest request,HttpSession session) {
		
		return "lyx";
	}
	
	@RequestMapping("/customer/addlyx.action")

	public String addlyx(HttpServletRequest request,HttpSession session) {
		 String ly = request.getParameter("lyx");
		System.out.println(ly);
		System.out.println(ly);
		System.out.println("以运行");
		return "lyx";
	}
	
}
